package com.zxw.设计模式.创建型.单例模式.饿汉式;

/**
 * @projectName: util-cloud
 * @package: com.zxw.设计模式.创建型.单例模式.饿汉式
 * @className: SingletonHungry
 * @author: zhangxuewei
 * @description: 饿汉模式
 * @date: 2023/6/9 17:22
 * @version: 1.0
 */
public class SingletonHungry {

    private String name;

    public String getName() {
        return name;
    }

    /**
     * 将构造方法私有化
     */
    private SingletonHungry() {
        // 防止反射创建单例对象
        if (singletonHungry != null)
            throw new RuntimeException("不允许非法访问");
    }

    private static SingletonHungry singletonHungry = new SingletonHungry();

    public static SingletonHungry getSingletonHungry() {
        singletonHungry.name = "zxw";
        return singletonHungry;
    }


}
